package com.smart.translator.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;
import androidx.appcompat.app.AlertDialog;
import android.util.Log;

public class PowerManagerHelper {
    private static final String TAG = "PowerManagerHelper";
    
    /**
     * 检查是否忽略电池优化
     */
    public static boolean isIgnoringBatteryOptimizations(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            if (powerManager != null) {
                return powerManager.isIgnoringBatteryOptimizations(context.getPackageName());
            }
        }
        return true; // Android 6.0以下默认忽略
    }
    
    /**
     * 请求忽略电池优化
     */
    public static void requestIgnoreBatteryOptimizations(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!isIgnoringBatteryOptimizations(activity)) {
                new AlertDialog.Builder(activity)
                    .setTitle("电池优化")
                    .setMessage("为了保持BLE连接稳定，建议忽略电池优化")
                    .setPositiveButton("去设置", (dialog, which) -> {
                        Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                        intent.setData(Uri.parse("package:" + activity.getPackageName()));
                        activity.startActivity(intent);
                    })
                    .setNegativeButton("取消", null)
                    .show();
            }
        }
    }
    
    /**
     * 检查是否支持Doze模式
     */
    public static boolean isDozeModeSupported(Context context) {
        try {
            PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            if (powerManager != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                // 使用反射调用isDeviceIdleModeSupported方法，因为它在某些版本中可能不可用
                try {
                    java.lang.reflect.Method method = powerManager.getClass().getMethod("isDeviceIdleModeSupported");
                    return (Boolean) method.invoke(powerManager);
                } catch (Exception e) {
                    Log.w(TAG, "isDeviceIdleModeSupported方法不可用", e);
                    return false;
                }
            }
            return false;
        } catch (Exception e) {
            Log.e(TAG, "检查Doze模式支持失败", e);
            return false;
        }
    }
    
    /**
     * 检查是否处于Doze模式
     */
    public static boolean isDeviceIdleMode(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            if (powerManager != null) {
                return powerManager.isDeviceIdleMode();
            }
        }
        return false;
    }
    
    /**
     * 检查是否处于交互状态
     */
    public static boolean isInteractive(Context context) {
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        if (powerManager != null) {
            return powerManager.isInteractive();
        }
        return true;
    }
    
    /**
     * 获取电源管理建议
     */
    public static String getPowerManagementAdvice(Context context) {
        StringBuilder advice = new StringBuilder();
        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!isIgnoringBatteryOptimizations(context)) {
                advice.append("建议忽略电池优化以保持BLE连接稳定\n");
            }
            
            if (isDeviceIdleMode(context)) {
                advice.append("设备处于Doze模式，可能影响BLE连接\n");
            }
        }
        
        if (!isInteractive(context)) {
            advice.append("设备屏幕关闭，可能影响BLE连接\n");
        }
        
        return advice.toString();
    }
} 